package com.hs.mod_archive.fragment

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.lifecycleScope
import com.hs.framework.base.BaseDataBindFragment
import com.hs.framework.toast.TipsToast
import com.hs.framework.utils.Speech
import com.hs.framework.utils.ValidateUtils
import com.hs.mod_archive.R
import com.hs.mod_archive.databinding.DialogAddReaderBinding
import com.hs.mod_archive.databinding.FragmentDevReaderConfigBinding
import com.hs.mod_archive.helper.DevReaderHelper
import com.hs.mod_archive.helper.ListViewHelper
import com.hs.mod_archive.helper.ListViewHelper.Companion.perPageCount
import com.hs.mod_archive.view.DialogUtil
import com.hs.mod_archive.view.MyDialog
import com.hs.room.model.archive.DevReader
import com.hs.room.repository.service.IDevRepository
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class FragmentDevReaderConfig : BaseDataBindFragment<FragmentDevReaderConfigBinding>() {
    private lateinit var ids: List<Int>
    private lateinit var datas: List<DevReader>
    private lateinit var names: List<String>

    @Inject
    lateinit var service: IDevRepository

    var dialogEdit: MyDialog? = null
    lateinit var helper: DevReaderHelper
    var dialogBinding: DialogAddReaderBinding? = null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        R.layout.dialog_add_reader
        mBinding!!.btAdd.setOnClickListener { showDialog() }
        helper =
            DevReaderHelper(
                mBinding!!.footPageListView, object : ListViewHelper.OnItemClickListener {
                    override fun onItemClickEdit(p: Int) {

                    }

                    override fun onItemClickDelete(p: Int) {
                        del(datas!!.get(p))
                    }
                },
                object : ListViewHelper.FootSearchCallback {
                    override fun onActionSearch(i: Int) {
                        goPage(i)
                    }
                })
    }

    private fun goPage(i: Int) {
        println("goPage:${i}")
        lifecycleScope.launch {
            service.getDevReader(i, perPageCount).collect {
                datas = it
                helper.setListDataAndRefresh(it)
            }
        }
    }

    private fun del(d: DevReader) {
        lifecycleScope.launch {
            service.delete(d)
            TipsToast.showTips("删除成功")
        }
    }


    override fun initData() {
        super.initData()
        lifecycleScope.launch {
            val count: Int = service.countDevReader()
            helper.setFootValue(count, 0)
            service.getDevReaderName().collect {
                names = it
            }
        }
        lifecycleScope.launch {
            service.getDevReaderID().collect {
                ids = it
            }
        }
        goPage(0)
    }

    private fun showDialog() {
        if (null == dialogBinding) {
            dialogBinding = DialogAddReaderBinding.inflate(layoutInflater)
        }
        dialogBinding!!.btSubmit.setOnClickListener { _ ->
            if (!ValidateUtils.notNull(
                    dialogBinding!!.name,
                    getString(R.string.name)
                ) || !ValidateUtils.notNull(
                    dialogBinding!!.readerMac,
                    getString(R.string.readerMac)
                )
            ) {
                return@setOnClickListener
            }
            val name: String = dialogBinding!!.name.text.toString().trim()
            if (names.contains(name)) {
                Speech.speak(getString(R.string.nameExist))
                return@setOnClickListener
            }
            val mac: String = dialogBinding!!.readerMac.text.toString().trim()
            if (ids.contains(mac.toInt())) {
                Speech.speak(getString(R.string.macExist))
                return@setOnClickListener
            }
            submit(DevReader(0, 0, name, mac.toInt(), 0))
        }
        val root: ConstraintLayout = dialogBinding!!.getRoot()
        val parent = root.parent
        if (null != parent) {
            val viewGroup = parent as ViewGroup
            viewGroup.removeView(root)
            dialogBinding!!.name.setText("")
            dialogBinding!!.readerMac.setText("")
        }
        dialogBinding!!.btClose.setOnClickListener { _ -> dismissDialog() }
        dialogBinding!!.btCancel.setOnClickListener { _ -> dismissDialog() }
        //-------------------------
        val w = resources.getDimensionPixelSize(R.dimen.dp_582)
        val h = resources.getDimensionPixelSize(R.dimen.dp_382)
        dialogEdit = DialogUtil.buildSpecialView(activity, root, w, h, false, false)
    }

    fun dismissDialog() {
        dialogEdit?.dismiss()
        dialogEdit = null
        dialogBinding = null
    }

    private fun submit(
        d: DevReader
    ) {
        insert(d)
        dismissDialog()
    }

    fun update(src: DevReader) {
        lifecycleScope.launch {
            service.update(src)
        }
    }

    fun insert(src: DevReader) {
        lifecycleScope.launch {
            service.insert(src)
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        helper.onDestroyView()
    }
}
